package com.hh.test.waitnotify;

public class Customer implements Runnable {

	public Customer(Stock stock) {
		super();
		this.stock = stock;
	}

	private Stock stock;

	public Stock getStock() {
		return stock;
	}

	public void setStock(Stock stock) {
		this.stock = stock;
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			synchronized (stock) {
				if (stock.getNum() == 0) {
					try {
						System.out.println("等待制造");
						stock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if (stock.getNum() > 0) {
					stock.setNum(stock.getNum() - 1);
					System.out.println("消费了一个商品");
					System.out.println("剩余：" + stock.getNum());
				}
			}
		}
	}

}
